#pragma once

#include "Structure.h"

namespace Kompex
{
	class SQLiteDatabase;
	class SQLiteStatement;
}

class Database
{
public:
	bool Connect();
	bool Close();

	void GetAllLabels(CNamesPtrArray &ary);
	void GetLabelClass(CStringArray &ary);
	void GetAllLibrary(CNamesPtrArray &ary);
	void GetAllFilePaths(CStringArray &ary);

	void GetFilesFromLabel(LPCTSTR labelName,CStringArray &ary);
	void GetLabelsFromFile(LPCTSTR fileName,CStringArray &ary);
	void GetRuleFromLibrary(LPCTSTR libraryName,CStringArray &ary);
	void GetFilesFromSetOperator(LPCTSTR sqlText,CStringArray &label,
		CStringArray& result);

	void AddLabel(LPCTSTR label);
	void AddLabel(LPCTSTR label,LPCTSTR labelclass);
	void AddLabelClass(LPCTSTR labelclass);
	void AddLibrary(LPCTSTR library,LPCTSTR rule);
	void RemoveFilePath(LPCTSTR path);

	void DeleteLabel(LPCTSTR label);
	void DeleteLabelClass(LPCTSTR labelclass);
	void DeleteLibrary(LPCTSTR library);

	void ChangeLabelName(LPCTSTR oldName, LPCTSTR newName);
	void ChangeLabelClassName(LPCTSTR oldName, LPCTSTR newName);
	void ChangeLabelClass(LPCTSTR label, LPCTSTR newClass);
	void ChangeLabelClassToNull(LPCTSTR label);
	void ChangeLibraryName(LPCTSTR oldName, LPCTSTR newName);
	void ChangeLibraryRule(LPCTSTR libraryName, LPCTSTR newRule);
	void ChangeFilePath(LPCTSTR oldPath,LPCTSTR newPath);

	void AddLabelToFile(LPCTSTR label, LPCTSTR file);
	void AddLabelsToFiles(CStringArray &labels, CStringArray &files);
	void AddLabelsToFile(CStringArray &labels, LPCTSTR file);
	void AddLabelToFiles(LPCTSTR label, CStringArray &files);

	void RemoveLabelFromFile(LPCTSTR label, LPCTSTR file);

	bool IsFileExists(LPCTSTR path);
private:
	bool buildDatabase();
	Kompex::SQLiteDatabase *pDatabase;
	Kompex::SQLiteStatement *pStmt;
	void createLogFile();
	inline bool isLogFileCreate();
	CStdioFile logFile;
};

bool Database::isLogFileCreate()
{
	return logFile.m_hFile != CFile::hFileNull;
}